package com.example.drawproject.View;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.example.drawproject.ItemClass.People;
import com.example.drawproject.R;
import com.example.drawproject.Utils.MoveUtils;

public class MyView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    private SurfaceHolder msurfaceholder;
    private Canvas mcanvas;
    private Paint paint;
    private boolean isDrawing;
    //当前坐标
    private float presentX;
    private float presentY;
    //目标坐标
    private float tox;
    private float toy;
    //速度
    private float add = 5;
    private int time = 150;
    private boolean isRight = true;

    private People people;
    private Bitmap bitmap;
    public MyView(Context context) {
        super(context);
        initView();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        msurfaceholder = getHolder();//获取SurfaceHolder对象
        msurfaceholder.addCallback(this);//注册SurfaceHolder的回调方法
        presentX = 0;
        presentY = 0;
        people = new People(getContext());
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isDrawing = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        isDrawing = false;
    }
//
//    @Override
//    public void run() {
//        int i = 0;
//        paint = new Paint();
//        while (isDrawing) {
//           if(toCoordinate[0] == touchX&& toCoordinate[1] == touchY){
//               toCoordinate = moveUtils.getEndCoordinate();
//           }
//            i = i + 1;
//            if (i == 7) {
//                i = 0;
//            }
//            if (Math.abs(toCoordinate[0] - touchX) < add) {
//                touchX = toCoordinate[0];
//            } else if (toCoordinate[0] > touchX) {
//                touchX += add;
//                if (point[i] != 0) {
//                    bitmaps[i] = leftRighBitmap(bitmaps[i]);
//                    point[i] = 0;
//                }
//            } else if (toCoordinate[0] < touchX) {
//                touchX -= add;
//                if (point[i] != 1) {
//                    bitmaps[i] = leftRighBitmap(bitmaps[i]);
//                    point[i] = 1;
//                }
//            }
//            if (Math.abs(toCoordinate[1] - touchY) < add) {
//                touchY = toCoordinate[1];
//            } else if (toCoordinate[1] > touchY) {
//                touchY += add;
//            } else if (toCoordinate[1] < touchY) {
//                touchY -= add;
//            }
//            Draw(i);
//
//            try {
//                Thread.sleep(130);
//            } catch (Exception e) {
//                Log.d("run", "error");
//            }
//        }
//    }

    @Override
    public void run() {
        paint = new Paint();
        while (isDrawing) {
            Move();

            bitmap = people.getMove();
            if(!isRight){
                bitmap = leftRighBitmap(bitmap);
            }
            Draw(bitmap);

            try {
                Thread.sleep(time);
            } catch (Exception e) {
                Log.d("run", "error");
            }
        }
    }

    private void Draw(Bitmap bitmap) {
        try {

            mcanvas = msurfaceholder.lockCanvas();
            mcanvas.drawColor(0, PorterDuff.Mode.CLEAR);
            mcanvas.drawBitmap(bitmap, presentX, presentY, paint);

        } catch (Exception e) {
        } finally {
            if (mcanvas != null)
                msurfaceholder.unlockCanvasAndPost(mcanvas);//保证每次都将绘图的内容提交
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        tox = event.getX();
        toy = event.getY();
        if((tox-presentX)*(tox-presentX)+(toy-presentY)*(toy-presentY) >= 250000){
            people.setStatus(2);
            add = 10;
        }else {
            people.setStatus(1);
            add = 5;
        }
        Log.d("Touch", "x = " + tox + "||y = " + toy);
        return true;
    }


    public Bitmap leftRighBitmap(Bitmap mBitmap) {
        Matrix m = new Matrix();
//        m.setScale(1, -1); // 垂直翻转
        m.setScale(-1, 1); // 水平翻转
        int w = mBitmap.getWidth();
        int h = mBitmap.getHeight();
        // 生成的翻转后的bitmap

        return Bitmap.createBitmap(mBitmap, 0, 0, w, h, m, true);
    }
    //计算移动坐标
    public void Move() {
        if (Math.abs(tox - presentX) < add) {
            presentX = tox;
        } else {
            if (tox > presentX) {
                isRight = true;
                presentX += add;

            } else if (tox < presentX) {
                isRight = false;
                presentX -= add;
            }
        }
        if (Math.abs(toy - presentY) < add) {
            presentY = toy;
        } else {
            if (toy > presentY) {
                presentY += add;

            } else if (toy < presentY) {
                presentY -= add;
            }
        }
    }
}
